<template>
  <div class="personal-info-container">
    <el-card class="info-card">
      <template #header>
        <div class="card-header">
          <span>个人信息（学生）</span>
          <el-button type="text" @click="refreshInfo">刷新</el-button>
        </div>
      </template>

      <div v-if="loading" class="loading">
        <el-skeleton :rows="4" animated />
      </div>

      <div v-else>
        <el-descriptions :column="2" border :label-style="{ width: '140px', fontWeight: '600' }">
          <el-descriptions-item label="姓名">
            {{ studentInfo?.name || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="学号">
            {{ studentInfo?.studentNum || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="班级">
            {{ studentInfo?.className || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="联系电话">
            {{ studentInfo?.phone || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="辅导员">
            {{ studentInfo?.counselor || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="核验状态" :span="1">
            <div class="status-tags">
              <el-tag size="small" :type="getStatusType(studentInfo?.nameCheckStatus)">姓名</el-tag>
              <el-tag size="small" :type="getStatusType(studentInfo?.studentNumCheckStatus)">学号</el-tag>
              <el-tag size="small" :type="getStatusType(studentInfo?.classCheckStatus)">班级</el-tag>
              <el-tag size="small" :type="getStatusType(studentInfo?.phoneCheckStatus)">电话</el-tag>
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="问题备注" :span="1">
            <span>{{ studentInfo?.problemMsg || '无' }}</span>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
  
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getStudentById } from '../../api/studentInfo'
import type { StudentInfo } from '../../api/studentInfo'

const loading = ref(false)
const studentInfo = ref<StudentInfo | null>(null)

const getLocalUserId = (): number | null => {
  try {
    const raw = localStorage.getItem('userInfo')
    const user = raw ? JSON.parse(raw) : null
    const id = Number(user?.userId)
    return Number.isFinite(id) && id > 0 ? id : null
  } catch (e) {
    return null
  }
}

const fetchInfo = async () => {
  const uid = getLocalUserId()
  if (!uid) {
    ElMessage.error('未获取到登录用户ID，无法查询学生信息')
    return
  }
  loading.value = true
  try {
    const resp = await getStudentById(uid)
    // 使用通用 ApiResponse：code 为字符串，消息字段为 msg
    if (resp?.code === '200') {
      studentInfo.value = resp?.data || null
    } else {
      ElMessage.error(resp?.msg || '获取学生信息失败')
    }
  } catch (err) {
    ElMessage.error('请求学生信息接口异常')
  } finally {
    loading.value = false
  }
}

const refreshInfo = () => fetchInfo()

const getStatusType = (status?: number) => {
  if (status === 1) return 'success'
  if (status === 2) return 'danger'
  return 'info'
}

onMounted(fetchInfo)
</script>

<style scoped>
.personal-info-container {
  padding: 10px;
}
.info-card {
  width: 100%;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.status-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.loading {
  padding: 16px;
}
</style>